<?php
// 会员管理，可CURD操作会员，
class MemberAction extends CommonAction {
    function _filter(&$map) {
         $key = trim($_REQUEST['keywords']);
         if(!empty($key)) {
             $map['name'] = array('like',"%".$key."%");
             $map['account'] = array('like',"%".$key."%");
             $map['email'] = array('like',"%".$key."%");
             $map['email1'] = array('like',"%".$key."%");
             $map['email2'] = array('like',"%".$key."%");
             $map['mobile'] = array('like',"%".$key."%");
             $map['mobile1'] = array('like',"%".$key."%");
             $map['mobile2'] = array('like',"%".$key."%");
             $map['telephone'] = array('like',"%".$key."%");
             $map['company'] = array('like',"%".$key."%");
             $map['payper_no'] = array('like',"%".$key."%");
             $map['remark'] = array('like',"%".$key."%");
             $map['qq'] = array('like',"%".$key."%");
             $map['msn'] = array('like',"%".$key."%");
             $map['company_address'] = array('like',"%".$key."%");
			 $map['fax'] = array('like',"%".$key."%");
             $map['gtalk'] = array('like',"%".$key."%");
             $map['skype'] = array('like',"%".$key."%");
			 $map['address'] = array('like',"%".$key."%");
			 $map['website'] = array('like',"%".$key."%");
			 $map['title'] = array('like',"%".$key."%");
			 $map['profile'] = array('like',"%".$key."%");
             $map['_logic'] = "or";
             //$map['profile'] =$key; //保证分页跳转正常,注意不要查询profile
         }
    }

    function _before_add(){
        $this->assign('paypers',C('Paypers'));
        $this->assign('rank',C('rank'));
        $this->assign('type',C('customer_type'));
        $this->assign('hotel',M('Hotel')->where('status = 1')->findAll());
    }

     function _before_edit(){
        $this->assign('payper_type',C('Paypers'));
        $this->assign('rank',C('rank'));
        $this->assign('type',C('customer_type'));
        $this->assign('hotel',M('Hotel')->where('status = 1')->findAll());
    }

    function _before_index(){
        $this->assign('type',C('customer_type'));
    }

    // 检查帐号
    public function checkAccount() {
        /*
        if(!preg_match('/^[a-z]\w{4,}$/i',$_POST['account'])) {
            $this->error( '用户名必须是字母，且5位以上！');
        }*/
        $Member = M("Member");
        // 检测用户名是否冲突
        $name  =  $_REQUEST['account'];
        $result  =  $Member->getByAccount($name);
        if($result) {
            $this->error('该用户名已经存在！');
        }else {
            $this->success('该用户名可以使用！');
        }
    }

    // 插入数据
    public function insert() {
        // 创建数据对象
        $Member	 =	 D("Member");
        if(!$Member->create()) {
            $this->error($Member->getError());
        }else {
            // 写入帐号数据
            if($result	 =	 $Member->add()) {
                $jumpUrl = $_REQUEST['jumpUrl'];
           if(empty($jumpUrl))
                $jumpUrl = Cookie::get ( '_currentUrl_' );
            $this->assign ( 'jumpUrl', $jumpUrl );

                $this->success('会员添加成功！');
            }else {
                $this->error('会员添加失败！');
            }
        }
    }

    //重置密码
    public function resetPwd() {
        $id  =  $_POST['id'];
        $password = $_POST['password'];
        if(''== trim($password)) {
            $this->error('密码不能为空！');
        }
        $User = M('Member');
        $User->password	=	md5($password);
        $User->id			=	$id;
        $result	=	$User->save();
        if(false !== $result) {
            $this->success("密码修改为$password");
        }else {
            $this->error('重置密码失败！');
        }
    }

    public function foreverdelete() {
        //删除指定记录
        $model = D ('Member');
        if (! empty ( $model )) {
            $pk = $model->getPk ();
            $id = $_REQUEST [$pk];
            if (isset ( $id )) {
                $condition = array ($pk => array ('in', explode ( ',', $id ) ) );
                //如果会员存在入住或预订，或留言记录，则不能删除
                $ids = explode(",", $id);
                foreach($ids as $member_id){
                    if($this->_exist_record($member_id)){
                        $this->error ('会员:'.getModelName($member_id, 'Member').' 存在历史记录，暂不能删除！');
                        exit;
                    }
                }
                
                if (false !== $model->where ( $condition )->delete ()) {
                    
                    $jumpUrl = $_REQUEST['jumpUrl'];
           if(empty($jumpUrl))
                $jumpUrl = Cookie::get ( '_currentUrl_' );
            $this->assign ( 'jumpUrl', $jumpUrl );
                    
                    $this->success ('删除成功！');
                } else {
                    $this->error ('删除失败！');
                }
            } else {
                $this->error ( '非法操作' );
            }
        }
        $this->forward ();
    }

    //查找该会员是否存在相关入住，或预订，或留言的记录，如是，返回true,否则返回false
    protected function _exist_record($member_id){
        $count_order = count_order($member_id);
        $count_checkin = count_checkin($member_id);
         $count_msg = count_msg($member_id);
         if( ($count_checkin+$count_msg+$count_order) > 0){
             return true;
         }else{
             return false;
         }

    }

}
?>